/*
服务端这边，创建了ServerSocket后
创建输入流，读取客户端过来的数据，然后保存到数组里，再创建输出流，循环把数组写入到新文件里
 */
package MySocket;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {
    public static void main(String[] args) {
        try(ServerSocket serversocket = new ServerSocket(8086)){
            Socket socket = serversocket.accept(); //等待接收连接
            InputStream inputStream = socket.getInputStream();  //获取输入流，从客户端过来
            FileOutputStream fileoutputStream = new FileOutputStream("net/test.txt");
            byte[] bytes = new byte[1024];
            int i;
            while ((i = inputStream.read(bytes)) != -1){  //循环读取输入流，读取到bytes里，读取长度是1024，如果返回-1，说明读完了
                fileoutputStream.write(bytes, 0 , i);  //循环把字节数组里的内容写到输出流
            }
            fileoutputStream.flush();
            fileoutputStream.close();

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
